package com.jing.controller;

import com.jing.pojo.ReservationInfo;
import com.jing.pojo.Result;
import com.jing.pojo.SensorSeatRequest;
import com.jing.service.ReservationInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 陈书憬
 * @version 1.0
 * 2024/9/20
 */
@RestController
@RequestMapping("/reservationInfo")
public class ReservationInfoController {

    @Autowired
    private ReservationInfoService reservationInfoService;


    /**
     * 检查座位有没有在数据库中
     *
     * @param reservationInfo
     * @return
     */
    @PostMapping("/checkSeatAvailability")
    public Result checkSeatAvailability(@RequestBody ReservationInfo reservationInfo) {
        System.out.println(reservationInfo);
        reservationInfoService.checkSeatAvailability(reservationInfo);
        // 处理逻辑
        return Result.success();
    }

    /**
     * 座位数据添加
     *
     * @param reservationInfo
     * @return
     */
    @PostMapping("/seatDataAdd")
    public Result add(@RequestBody @Validated ReservationInfo reservationInfo) {
        System.out.println(reservationInfo);
        reservationInfoService.add(reservationInfo);
        return Result.success();
    }


    /**
     * 传感器座位预约
     */
//    @PostMapping("/addSensorSeat")
//    public Result addSensorSeat(@RequestBody  String name, String time, String seatId, String username) {
//        System.out.println("username: " + username);
//        System.out.println("name: " + name);
//        System.out.println("time: " + time);
//        System.out.println("seatId: " + seatId);
//
//        reservationInfoService.addSensorSeat(name, time, seatId, username);
//        return Result.success();
//    }
    @PostMapping("/addSensorSeat")
    public Result addSensorSeat( String roomName, String time, String seatId, String username) {
//        String username = request.getUsername();
//        String roomName = request.getRoomName();
//        String time = request.getTime();
//        String seatId = request.getSeatId();

        System.out.println("username: " + username);
        System.out.println("name: " + roomName);
        System.out.println("time: " + time);
        System.out.println("seatId: " + seatId);

        reservationInfoService.addSensorSeat(roomName, time, seatId, username);
        return Result.success();
    }



    /**
     * 查找这个自习室中的座位的状态是否'已预约', '进行中'
     *
     * @param id
     * @param seatId
     * @return
     */
    @GetMapping("/findSeat")
    public Result<ReservationInfo> findSeat(Integer id, Integer seatId) {
        System.out.println(id + "...." + seatId);
        ReservationInfo reservationInfo = reservationInfoService.findSeat(id, seatId);
        return Result.success(reservationInfo);
    }


    /**
     * 查找这个自习室中的座位的状态是否'已预约', '进行中'
     *
     * @param id
     * @param seatId
     * @return
     */
    @GetMapping("/findSeatTimes")
    public Result<List<ReservationInfo>> findSeatTimes(Integer id, Integer seatId) {
        System.out.println(id + "...." + seatId);
        List<ReservationInfo> reservationInfo = reservationInfoService.findSeatTimes(id, seatId);
        System.out.println(reservationInfo);
        return Result.success(reservationInfo);
    }


    /**
     * 查找这个自习室中的座位的状态是否'已预约', '进行中'
     *
     * @param id
     * @return
     */
    @GetMapping("/findSeatButton")
    public Result<List<Integer>> findSeatButton(Integer id) {
        List<Integer> seatButton = reservationInfoService.findSeatButton(id);
        return Result.success(seatButton);
    }


    /**
     * 查看用户是否有座位，这里返回座位状态
     *
     * @param id
     * @return
     */
    @GetMapping("/findUserSeat")
    public Result<List<String>> findUserSeat(Integer id) {
        // 假设 findUserSeat 方法现在只返回一个座位状态
        List<String> seatStatus = reservationInfoService.findUserSeatStatus(id);
        return Result.success(seatStatus);
    }


    /**
     * `根据id查找相关预约信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/findUserMsgById")
    public Result<List<ReservationInfo>> findUserMsgById(Integer userId) {
        // 假设 findUserSeat 方法现在只返回一个座位状态
        List<ReservationInfo> reservationInfoList = reservationInfoService.findUserMsgById(userId);
        return Result.success(reservationInfoList);
    }

    /**
     * 签到
     *
     * @param userId
     * @param checkInTime
     * @return
     */
    @PutMapping("/updateSignIn")
    public Result updateSignIn(Integer userId, String checkInTime) {
        reservationInfoService.updateSignIn(userId, checkInTime);
        System.out.println("签到成功了");
        return Result.success();
    }


    /**
     * 签退
     *
     * @param userId
     * @param checkOutTime
     * @retur
     */
    @PutMapping("/updateSignOut")
    public Result updateSignOut(Integer userId, String checkOutTime) {
        reservationInfoService.updateSignOut(userId, checkOutTime);
        System.out.println("签退成功了");
        return Result.success();
    }

    /**
     * 修改用户状态
     *
     * @param userId
     * @param reservationStatus
     * @retur
     */
    @PutMapping("/setSeatStatus")
    public Result setSeatStatus(Integer userId, String reservationStatus) {
        reservationInfoService.setSeatStatus(userId, reservationStatus);
        System.out.println("用户状态修改成功！");
        return Result.success();
    }

    /**
     * 删除用户基本信息
     *
     * @param userId
     * @return
     */
    @DeleteMapping("/deleteUserInfo")
    public Result deleteUserInfo(Integer userId) {
        reservationInfoService.deleteUserInfo(userId);
        System.out.println("用户删除成功！");
        return Result.success();
    }


    /**
     * 查看座位状态
     *
     * @param userId
     * @return
     */
    @GetMapping("/selectSeatStatus")
    public Result selectSeatStatus(Integer userId) {
        reservationInfoService.selectSeatStatus(userId);
        System.out.println("查询成功！");
        return Result.success();
    }


    /**
     * 更换座位
     *
     * @param reservationInfo
     * @return
     */
    @PatchMapping("/changeSeatMsgService")
    public Result changeSeatMsg(@RequestBody @Validated ReservationInfo reservationInfo) {
        reservationInfoService.changeSeatMsg(reservationInfo);
        return Result.success();
    }

}
